﻿#region usings
using System;
using System.Windows.Media;
#endregion

namespace Papa.Common {
    /// <summary>
    /// Provides additional conversions
    /// </summary>
    /// <remarks>Could make for an interesting extension method to Color</remarks>
    public class ColorConverter {
        /// <summary>
        /// Returns a SolidColorBrush from a hex value. 
        /// Useful when you need to serialize the color for style settings
        /// </summary>
        /// <param name="myColor"></param>
        /// <returns></returns>
        public static SolidColorBrush GetColorFromHex(string hexColor) {
            SolidColorBrush retVal;
            try {
                retVal = new SolidColorBrush(
                    Color.FromArgb(
                        Convert.ToByte(hexColor.Substring(1, 2), 16),
                        Convert.ToByte(hexColor.Substring(3, 2), 16),
                        Convert.ToByte(hexColor.Substring(5, 2), 16),
                        Convert.ToByte(hexColor.Substring(7, 2), 16)));

            } catch {

                retVal = new SolidColorBrush(Colors.Red);
            }
            return retVal;
        }
    
        public static string GetHexFromColor(SolidColorBrush brush) {
            System.Text.StringBuilder hexValue = new System.Text.StringBuilder("#", 9);
            hexValue.Append(string.Format("{0:x2}", brush.Color.A));
            hexValue.Append(string.Format("{0:x2}", brush.Color.R));
            hexValue.Append(string.Format("{0:x2}", brush.Color.G));
            hexValue.Append(string.Format("{0:x2}", brush.Color.B));
            return hexValue.ToString().ToUpper();
            
            
        }
    }
}
